#include "stdafx.h"
#include "net/DatagramSocket.hpp"
#include "net/DatagramSocketImpl.hpp"

NET_NAMESPACE_BEGIN

CDatagramSocket::CDatagramSocket() : CSocket(new CDatagramSocketImpl)
{
}

CDatagramSocket::CDatagramSocket(CIPAddress::Family family) : CSocket(new CDatagramSocketImpl(family))
{
}

CDatagramSocket::CDatagramSocket(const CSocketAddress& address, bool reuseAddress)
	: CSocket(new CDatagramSocketImpl(address.family()))
{
	bind(address, reuseAddress);
}

CDatagramSocket::CDatagramSocket(const CSocket& socket) : CSocket(socket)
{
	if (!dynamic_cast<CDatagramSocketImpl*>(impl()))
		throw InvalidArgumentException("Cannot assign incompatible socket");
}

CDatagramSocket::CDatagramSocket(CSocketImpl* pImpl) : CSocket(pImpl)
{
	if (!dynamic_cast<CDatagramSocketImpl*>(impl()))
		throw InvalidArgumentException("Cannot assign incompatible socket");
}

CDatagramSocket::~CDatagramSocket()
{
}

CDatagramSocket& CDatagramSocket::operator = (const CSocket& socket)
{
	if (dynamic_cast<CDatagramSocketImpl*>(socket.impl()))
		CSocket::operator = (socket);
	else
		throw InvalidArgumentException("Cannot assign incompatible socket");
	return *this;
}

void CDatagramSocket::connect(const CSocketAddress& address)
{
	impl()->connect(address);
}

void CDatagramSocket::bind(const CSocketAddress& address, bool reuseAddress)
{
	impl()->bind(address, reuseAddress);
}

int CDatagramSocket::sendBytes(const void* buffer, int length, int flags)
{
	return impl()->sendBytes(buffer, length, flags);
}

int CDatagramSocket::receiveBytes(void* buffer, int length, int flags)
{
	return impl()->receiveBytes(buffer, length, flags);
}

int CDatagramSocket::sendTo(const void* buffer, int length, const CSocketAddress& address, int flags)
{
	return impl()->sendTo(buffer, length, address, flags);
}

int CDatagramSocket::receiveFrom(void* buffer, int length, CSocketAddress& address, int flags)
{
	return impl()->receiveFrom(buffer, length, address, flags);
}

void CDatagramSocket::setBroadcast(bool flag)
{
	impl()->setBroadcast(flag);
}

bool CDatagramSocket::getBroadcast() const
{
	return impl()->getBroadcast();
}

NET_NAMESPACE_END
